package com.itheima.zhuanhuan;

import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import com.itheima.domain.Student;

/*
- 创建一个TreeMap集合,键是学生对象(Student),值是籍贯(String),学生属性姓名和年龄,按照年龄进行排序并遍历
- 要求按照学生的年龄进行排序,如果年龄相同则按照姓名进行排序

 */
public class Test04 {

    public static void main(String[] args) {

        TreeMap<Student, String> map = new TreeMap<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int result = o1.getAge() - o2.getAge();
                result = result == 0 ? o1.getName().compareTo(o2.getName()) : result;
                return result;
            }
        });


        Student lu = new Student("鹿晗", 28);
        Student guan = new Student("关晓彤", 22);
        Student wu = new Student("吴亦凡", 27);
        Student cai = new Student("蔡徐坤", 28);
        map.put(lu,"北京海淀");
        map.put(guan,"北京西城区");
        map.put(wu,"山西太原");
        map.put(cai,"山西运城");

        //第一种 keyset

        Set<Student> students = map.keySet();
        for (Student student : students) {
            String home = map.get(student);
            System.out.println(student+"===他家在"+home);
        }
        //第二种 entrySet
        Set<Map.Entry<Student, String>> entries = map.entrySet();
        for (Map.Entry<Student, String> entry : entries) {
            Student student = entry.getKey();
            String home = entry.getValue();
            System.out.println(
                    student+"========他家在"+home
            );

        }

        System.out.println("蔡徐坤".compareTo("鹿晗"));
    }
}
